//
//  XJBuyNumView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJBuyNumView.h"
@interface XJBuyNumView()
{
    NSInteger _cnt;
}
@property(nonatomic, strong) UILabel *buyCountTitleLb; // 购买数量
@property(nonatomic, strong) UIButton *minusBtn; // 减少btn
@property(nonatomic, strong) UILabel *buyNumLb; // 购买数量
@property(nonatomic, strong) UIButton *addBtn; // 增加btn

@end

@implementation XJBuyNumView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

// frame 是写死的 CGRectMake(0, 100, KWidth, 50)
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 默认购买数量为1
        _cnt = 1;
        
        [self setupSubView];
    }
    return self;
}

#pragma mark - UI
- (void)setupSubView {
 // frame 是写死的 CGRectMake(0, 100, KWidth, 50)
    [self addSubview:self.buyCountTitleLb];
    
    [self addSubview:self.addBtn];
    [self addSubview:self.buyNumLb];
    [self addSubview:self.minusBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat magin = 5;
    CGFloat w = 40;
    CGFloat centerY = (50 - 40) / 2;
    self.buyCountTitleLb.frame = CGRectMake(magin*4, centerY, 80, w);
 
    self.minusBtn.frame = CGRectMake(KWidth-150, centerY, w, w);
 
    self.buyNumLb.frame = CGRectMake(KWidth-105, centerY, w, w);
 
    self.addBtn.frame = CGRectMake(KWidth - 60, centerY, w, w);
}

#pragma mark - lazy -
- (UILabel *)buyCountTitleLb {
    if (!_buyCountTitleLb) {
        _buyCountTitleLb = [UILabel new];
        _buyCountTitleLb.text = @"购买数量";
        _buyCountTitleLb.textColor = [UIColor lightGrayColor];
        _buyCountTitleLb.font = XJFONTSET_R(14);
    }
    return _buyCountTitleLb;
}

- (UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_minusBtn setImage:[UIImage imageNamed:@"number_goods_reduce"] forState:(UIControlStateNormal)];
        [_minusBtn addTarget:self action:@selector(minusClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _minusBtn;
}


// 购买数量
- (UILabel *)buyNumLb {
    if (!_buyNumLb) {
        _buyNumLb = [UILabel new];
        _buyNumLb.text = @"1"; // 默认1
        _buyNumLb.textAlignment = NSTextAlignmentCenter;
    }
    return _buyNumLb;
}

- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_addBtn setImage:[UIImage imageNamed:@"number_goods_add"] forState:(UIControlStateNormal)];
        [_addBtn addTarget:self action:@selector(addClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _addBtn;
}

#pragma mark - 点击事件
- (void)minusClickAction:(UIButton *)btn {
    
    _cnt--;
    if (_cnt<1) {
        _cnt = 1;
    }
    self.buyNumLb.text  = [NSString stringWithFormat:@"%ld",_cnt];;
    
//    if (self.buyCountBlock) {
//        self.buyCountBlock(_cnt);
//    }
 
    if ([self.delegate respondsToSelector:@selector(xj_buyedCount:)]) {
        [self.delegate xj_buyedCount:_cnt];
    }

}

- (void)addClickAction:(UIButton *)btn {    
    _cnt++;
    
//    if (self.buyCountBlock) {
//        self.buyCountBlock(_cnt);
//    }
    self.buyNumLb.text  = [NSString stringWithFormat:@"%ld",_cnt];;

    if ([self.delegate respondsToSelector:@selector(xj_buyedCount:)]) {
        [self.delegate xj_buyedCount:_cnt];
    }
}
@end
